define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'games/game_info/index' + location.search,
                    add_url: 'games/game_info/add',
                    edit_url: 'games/game_info/edit',
                    del_url: 'games/game_info/del',
                    multi_url: 'games/game_info/multi',
                    import_url: 'games/game_info/import',
                    table: 'game_info',
                }
            });

            var table = $("#table");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='game.game']", form).addClass("selectpage").data("source", "games/game/index").data("primaryKey", "game").data("field", "game").data("orderBy", "id desc");
                // $("input[name='game.game']").attr("value","地下城与勇士");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
                $("input[name='game.game']").attr("value","地下城与勇士");
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                pagination:false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'game.game', title: __('Game_id'), operate: 'like'},
                        {field: 'gametype.title', title: __('添加类别'), operate: false},
                        // {field: 'parent.name', title: __('Pid')},
                        {field: 'name', title: __('Name'), align: 'left', formatter: Controller.api.formatter.name, operate: false},
                        {field: 'status', title: __('Status'), operate: 'LIKE', formatter: Table.api.formatter.status, operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();

        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                name: function (value, row, index) {
                    value = value.toString().replace(/(&|&amp;)nbsp;/g, '&nbsp;');
                    return !row.ismenu || row.status == 'hidden' ? "<span class='text-muted'>" + value + "</span>" : value;
                },

            },
            bindevent: function () {
                $('.game_id').on('change', function (e) {
                    var game_id = $('.game_id').val();
                    var id = $('#id').val();
                    $.post("games/game_info/getsku",{game_id:game_id,id:id},function(result){
                        // console.log(result);
                        // $("#sku").remove();
                        $("#sku").empty();
                        $("#sku").append(result.data);
                    });

                })
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
